package com.example.myhelloworld.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;


//CorrectHandler listens to a component inside an exercise (i.e. a 'correct' or 'close' button ). 
//When that button is clicked, a result is demanded from the exercise, the received result is sent
//to the server for statistics and result storage.
class CorrectHandler implements ClickHandler ,KeyUpHandler{
	GreetingServiceAsync gsa;
	ExerciseClass ex;
	String exerciseName;
	String userAndHash;
	CorrectHandler(GreetingServiceAsync g, ExerciseClass exCl, String exName,String userNameAndHash){
		gsa = g;
		ex = exCl;
		exerciseName = exName;
		userAndHash = userNameAndHash;
	}
	public void onClick(ClickEvent event) {
		System.out.println("Do we get here? " + event.getSource().getClass());
//		just a random answer response, we just send it away, not caring about any response
		
		ex.getSelfPanel().removeFromParent();
		System.out.println("Correct handler reached...");
		gsa.sendResult((userAndHash + "," + exerciseName + "," + ex.pushresult()), new AsyncCallback<String>(){

			public void onFailure(Throwable caught) {}

			public void onSuccess(String result) {	
			}});
	}

	public void onKeyUp(KeyUpEvent event) {
		System.out.println( event.getNativeKeyCode() + " + "  + event.getSource().getClass());
	}
	
	
}